<?php


namespace app\common\lib;


class Arr
{
    public static function getTree($data)
    {

        //将分类ID作为下标 组装新数据
        $items = [];
        foreach ($data as $v) {
            $v['category_id'] = $v['id'];
            unset($v['id']);
            $items[$v['category_id']] = $v;
        }

        $tree = [];

        foreach ($items as $id => $item) {
            if (isset($items[$item['pid']])) {   //判断该分类是否有pid
                $items[$item['pid']]['list'][] = &$items[$id]; //有pid则则将该分类push进入父分类的list下标中
            } else {
                $tree[] = &$items[$id];     //没有父分类直接push进入数组
            }
        }

        return $tree;
    }
}